package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.ISimuatorStatusInfoDao;
import org.jsbd.boss.domian.SimuatorStatusInfo;
import org.jsbd.boss.service.ISimuatorStatusInfoService;
import org.jsbd.boss.util.CacheKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("simuatorStatusInfoService")
public class SimuatorStatusInfoServiceImpl implements ISimuatorStatusInfoService{
	public static final Logger LOGGER = LoggerFactory.getLogger(ISimuatorStatusInfoService.class);
	
	@Autowired
	private ISimuatorStatusInfoDao simuatorStatusInfoDao;
	@Autowired
	private RedisClient<String,Object> redisClient;
	
	@Override
	public List<SimuatorStatusInfo> getSimulatorList(SimuatorStatusInfo simuatorStatusInfo) {
		LOGGER.info("condition is [{}]", simuatorStatusInfo);
		return simuatorStatusInfoDao.getSimulatorList(simuatorStatusInfo);
	}

	@Override
	public Integer getListPageCount(SimuatorStatusInfo simuatorStatusInfo) {
		return simuatorStatusInfoDao.getSimulatorListCount(simuatorStatusInfo);
	}

	@Override
	public void updateByUuid(List<SimuatorStatusInfo> simLists) {
	    simuatorStatusInfoDao.updateByUuid(simLists);
	}

	@Override
	public String getCabientByIp(String clientIp) {
		return simuatorStatusInfoDao.findCabinetByIp(clientIp);
	}

	@Override
	public void updateStatusByUuid(List<SimuatorStatusInfo> simLists) {
		simuatorStatusInfoDao.updateStatusByUuid(simLists);
	}

	@Override
	public void syncStatus() {
		String ipAndPortKey=CacheKey.CLMAP_IP_AND_PORT_KEY;
		String statusKey=CacheKey.CLADJUST_STATUS_HASH_KEY;
		Map<Object, Object> keepMap= redisClient.getHash(ipAndPortKey);
		redisClient.putHashMap(statusKey, keepMap);
	}

	@Override
	public void clearOffline(String uuids) {
		String[] uuidArr=uuids.split(",");
		String prefixKey=CacheKey.MARK_DO_LOG_KEY;
		List<String> uuidList=new ArrayList<>();
		for(String uuid:uuidArr) {
			if(StringUtils.isBlank(uuid)||"null".equals(uuid)) {
				continue;
			}
			redisClient.remove(prefixKey+uuid);
			uuidList.add(uuid);
		}
		
		if(uuidList.isEmpty()) {
			return;
		}
		simuatorStatusInfoDao.deleteOfflineByUuid(uuidList);
	}

	@Override
	public void clearOfflineForVer(String bizVer) {
		SimuatorStatusInfo statusInfo=new SimuatorStatusInfo();
		statusInfo.setBizVer(bizVer);
		statusInfo.setStatus(2);
		List<SimuatorStatusInfo> statusList=simuatorStatusInfoDao.getSimulatorList(statusInfo);
	    if(statusList==null||statusList.isEmpty()) {
	    	return;
	    }
	    String prefixKey=CacheKey.MARK_DO_LOG_KEY;
		for(SimuatorStatusInfo info:statusList) {
			redisClient.remove(prefixKey+info.getUuid());
		}
		
		simuatorStatusInfoDao.deleteOfflineByBizVer(bizVer);
	}

	@Override
	public void checkBusy(long busyTime) {
		String key=CacheKey.MAP_IP_AND_PORT_KEY;
		Map<Object,Object> nodeMap=redisClient.getHash(key);
		if(nodeMap==null||nodeMap.isEmpty()) {
			return;
		}
		for(Entry<Object, Object> entity:nodeMap.entrySet()) {
			String ipAndPort=(String)entity.getKey();
			redisClient.put(CacheKey.CHECK_BUSY_KEY+ipAndPort, busyTime);
		}
	}

	@Override
	public List<SimuatorStatusInfo> selectByModel(String model) {
		return simuatorStatusInfoDao.selectByModel(model);
	}

}
